package com.wl.zlmanage.controller;

import com.wl.zlmanage.bean.Dept;
import com.wl.zlmanage.bean.Role;
import com.wl.zlmanage.bean.User;
import com.wl.zlmanage.mapper.UserMapper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by wanglei on 2018/5/25.
 */
@Controller
public class UserController {

    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    UserMapper userDao;

    @RequestMapping("/getUserList.do")
    @ResponseBody
    public Map<String, Object> getUserList(@RequestParam("page") int page,
        @RequestParam("limit") int limit, @RequestParam("search") String search) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        int startIndex = (page - 1) * limit;
        List<User> reList = userDao.getUserList(startIndex,limit,search);
        Integer count = userDao.getUserListCount(search);
        resultMap.put("data", reList);
        resultMap.put("count", count);
        resultMap.put("code", 0);
        resultMap.put("msg", "");
        return resultMap;
    }

    @RequestMapping("/getAllRole.do")
    @ResponseBody
    public List<Role> getAllRole() {
        List<Role> reList = userDao.getAllRole();
        return reList;
    }

    @RequestMapping("/getAllDept.do")
    @ResponseBody
    public List<Dept> getAllDept() {
        List<Dept> reList = userDao.getAllDept();
        return reList;
    }

    @RequestMapping("/saveUser.do")
    @ResponseBody
    public String saveUser(User user) {
        userDao.saveUser(user);
        //如果是配证角色想配证人员表里面添加一条数据
        if(7 == user.getRoleId()){
            userDao.addMatchId(user.getId());
        }
        return "success";
    }

    @RequestMapping("/editUser.do")
    @ResponseBody
    public String editUser(User user) {
        userDao.editUser(user);
        return "success";
    }

    @RequestMapping("/deleteUser.do")
    @ResponseBody
    public String deleteUser(@RequestParam("id") Integer id) {
        userDao.deleteUser(id);
        return "success";
    }

    @RequestMapping("/getUserById.do")
    @ResponseBody
    public User getUserById(@RequestParam("id") Integer id) {
        User user = userDao.getUserByUserId(id);
        return user;
    }

    @RequestMapping("/getUserMatchIds.do")
    @ResponseBody
    public String getUserMatchIds(HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        List<Integer> idsList = userDao.getUserMatchIds();
        if(idsList.contains(userId)){
            return "success";
        }
        return "fail";
    }

    @RequestMapping("/updatePass.do")
    @ResponseBody
    public String updatePass(@RequestParam("userId") Integer userId,@RequestParam("password") String password) {
        userDao.updatePass(userId,password);
        return "success";
    }

    @RequestMapping("/getAllUser.do")
    @ResponseBody
    public List<User> getAllUser() {
        List<User> userList = userDao.getAllUser();
        return userList;
    }

    @RequestMapping("/transferData.do")
    @ResponseBody
    public String transferData(@RequestParam("srcUserId") Integer srcUserId,
        @RequestParam("targetUserId") Integer targetUserId) {
        userDao.updateZlInfoUserId(srcUserId,targetUserId);
        logger.info("资源数据转移成功！");
        userDao.updateZlSignInfoUserId(srcUserId,targetUserId);
        logger.info("签约客户数据转移成功！");
        userDao.updateApprovalInfoUserId(srcUserId,targetUserId);
        logger.info("签约客户审批数据转移成功！");
        userDao.updateApplyApprovalInfoUserId(srcUserId,targetUserId);
        logger.info("转回款审批数据转移成功！");
        userDao.updateExpendApprovalInfoUserId(srcUserId,targetUserId);
        logger.info("签约报销审批数据转移成功！");
        userDao.updateExpendApprovalInfoNewUserId(srcUserId,targetUserId);
        logger.info("未签约报销审批数据转移成功！");
        userDao.updateExpendInfoNewUserId(srcUserId,targetUserId);
        logger.info("未签约报销数据转移成功！");
        userDao.updateKitUserId(srcUserId,targetUserId);
        logger.info("附件数据转移成功！");
        userDao.updateSystemKitUserId(srcUserId,targetUserId);
        logger.info("系统附件数据转移成功！");
        return "success";
    }


}
